<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>web socket demo</title>
</head>
<body>


<script>
    //加密的websocket以wss开头
    var ws = new WebSocket("wss://echo.websocket.org");

    ws.onopen = function(evt) {
        console.log("Connection open ...");
        ws.send("Hello WebSockets!");
    };

    //当浏览器端的websocket收到消息时
    ws.onmessage = function(evt) {
        console.log( "Received Message: " + evt.data);
        ws.close();
    };

    ws.onclose = function(evt) {
        console.log("Connection closed.");
    };
</script>
</body>
</html>